package com.imufe.chemicalenterprisedangerwarning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imufe.chemicalenterprisedangerwarning.entity.po.SafetySupervisionBureauPO;
import com.imufe.chemicalenterprisedangerwarning.entity.query.SafetySupervisionBureauQuery;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.SafetySupervisionBureauDetailedVO;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.SafetySupervisionBureauVO;
import com.imufe.chemicalenterprisedangerwarning.mapper.RegionMapper;
import com.imufe.chemicalenterprisedangerwarning.mapper.SafetySupervisionBureauMapper;
import com.imufe.chemicalenterprisedangerwarning.service.RegionService;
import com.imufe.chemicalenterprisedangerwarning.service.SafetySupervisionBureauService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imufe.chemicalenterprisedangerwarning.util.BeanUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 安监局 服务实现类
 * </p>
 *
 * @author BaiBan
 * @since 2022-09-23
 */
@Service
@Log4j2
public class SafetySupervisionBureauServiceImpl extends ServiceImpl<SafetySupervisionBureauMapper, SafetySupervisionBureauPO> implements SafetySupervisionBureauService {

    @Autowired
    SafetySupervisionBureauMapper mapper;

    @Autowired
    RegionMapper regionMapper;

    @Autowired
    RegionService regionService;

    @Override
    public SafetySupervisionBureauVO basic(Long id) {
        SafetySupervisionBureauPO po = mapper.selectById(id);
        return BeanUtils.copyPOJO(po, SafetySupervisionBureauVO.class);
    }

    @Override
    public SafetySupervisionBureauDetailedVO detailed(Long id) {
        SafetySupervisionBureauPO po = mapper.selectById(id);
        // 基本信息填充
        SafetySupervisionBureauDetailedVO vo = BeanUtils.copyPOJO(po, SafetySupervisionBureauDetailedVO.class);

        // 填充根路径
        vo.setRegion(regionService.detailed(po.getRegionId()));
        return vo;
    }

    @Override
    public IPage<SafetySupervisionBureauVO> page(Long current) {
        Page<SafetySupervisionBureauVO> page = new Page<>(current, 10);
        mapper.page(page, new QueryWrapper<>());
        return page;
    }

    @Override
    public SafetySupervisionBureauVO getByRegion(Long id) {
        SafetySupervisionBureauPO po;
        // 查询当前区域又没有安监局
        do {
            QueryWrapper<SafetySupervisionBureauPO> qw = new QueryWrapper<>();
            qw.eq("region_id", id);

            po = mapper.selectOne(qw);

            // id更新到上级区域
            id = regionMapper.selectById(id).getSuperId();
        } while (po == null);

        // po 转 vo
        SafetySupervisionBureauVO vo = BeanUtils.copyPOJO(po, SafetySupervisionBureauVO.class);
        log.info(vo);

        return vo;
    }

    @Override
    public SafetySupervisionBureauDetailedVO create(SafetySupervisionBureauQuery query) {
        SafetySupervisionBureauPO po = BeanUtils.copyPOJO(query, SafetySupervisionBureauPO.class);
        log.info(po);
        mapper.insert(po);
        return this.detailed(po.getId());
    }

    @Override
    public SafetySupervisionBureauDetailedVO update(SafetySupervisionBureauQuery query) {
        SafetySupervisionBureauPO po = BeanUtils.copyPOJO(query, SafetySupervisionBureauPO.class);
        log.info(po);
        mapper.updateById(po);
        return this.detailed(po.getId());
    }
}
